package com.example.myapplication;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class ImageActivity extends AppCompatActivity implements View.OnClickListener {
    private  ImageView iv_scale;
    private Button btn_cam;
    private ImageView iv_capture;
    private Drawable drawable;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_page);
        iv_scale  = findViewById(R.id.iv_scale);
        findViewById(R.id.img_01).setOnClickListener(this);
        findViewById(R.id.img_02).setOnClickListener(this);
        findViewById(R.id.img_03).setOnClickListener(this);
        findViewById(R.id.img_04).setOnClickListener(this);
        findViewById(R.id.img_05).setOnClickListener(this);
        findViewById(R.id.img_06).setOnClickListener(this);
        findViewById(R.id.img_07).setOnClickListener(this);
        findViewById(R.id.btn_cam).setOnClickListener(this);
        btn_cam = findViewById(R.id.btn_cam);
        iv_capture = findViewById(R.id.iv_capture);
        btn_cam.setDrawingCacheEnabled(true);
        iv_scale.setDrawingCacheEnabled(true);
        findViewById(R.id.tv_top).setOnClickListener(this);
        findViewById(R.id.tv_bottom).setOnClickListener(this);
        findViewById(R.id.tv_left).setOnClickListener(this);
        findViewById(R.id.tv_right).setOnClickListener(this);
        drawable = getResources().getDrawable(R.mipmap.ic_launcher);
        drawable.setBounds(0,0,20,20);
    }

    @Override
    public void onClick(View v){
        if(v.getId()==R.id.img_01){
            Log.v("CENTER","CENTER");/*放大了，很奇怪*/
            iv_scale.setScaleType(ImageView.ScaleType.CENTER);
        }else if(v.getId()==R.id.img_02){
            Log.v("FIT_CENTER","FIT_CENTER");/*宽度填满，位置中部*/
            iv_scale.setScaleType(ImageView.ScaleType.FIT_CENTER);
        }else if(v.getId()==R.id.img_03){
            Log.v("CENTER_CROP","CENTER_CROP");/*高度填满，宽度多余裁剪*/
            iv_scale.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }else if(v.getId()==R.id.img_04){
            Log.v("CENTER_INSIDE","CENTER_INSIDE");/*压缩填满，会变形*/
            iv_scale.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        }else if(v.getId()==R.id.img_05){
            Log.v("FIT_XY","FIT_XY");/*压缩填满，会变形*/
            iv_scale.setScaleType(ImageView.ScaleType.FIT_XY);
        }else if(v.getId()==R.id.img_06){
            Log.v("FIT_START","FIT_START");/*宽度填满，位置头部*/
            iv_scale.setScaleType(ImageView.ScaleType.FIT_START);
        }else if(v.getId()==R.id.img_07){
            Log.v("FIT_END","FIT_END");/*宽度填满，位置尾部*/
            iv_scale.setScaleType(ImageView.ScaleType.FIT_END);
        }else if(v.getId() == R.id.btn_cam){
            Bitmap bitmap = iv_scale.getDrawingCache();
            iv_capture.setImageBitmap(bitmap);
            mHandler.postDelayed(mResetCache,200);
        }else if(v.getId()==R.id.tv_top){
            btn_cam.setCompoundDrawables(null,drawable,null,null);
        }else if(v.getId()==R.id.tv_bottom){
            btn_cam.setCompoundDrawables(null,null,null,drawable);
        }else if(v.getId()==R.id.tv_left){
            btn_cam.setCompoundDrawables(drawable,null,null,null);
        }else if(v.getId()==R.id.tv_right){
            btn_cam.setCompoundDrawables(null,null,drawable,null);
        }
    }
    private final Handler mHandler = new Handler();
    private final Runnable mResetCache = new Runnable() {
        @Override
        public void run() {
            btn_cam.setDrawingCacheEnabled(false);
            btn_cam.setDrawingCacheEnabled(true);
        }
    };
}
